package com.fbinggun.dt;


import com.fbinggun.dt.player.PlayerData;
import com.fbinggun.dt.task.Task;
import com.fbinggun.dt.task.conditions.Conditions;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;

import java.sql.SQLException;

public class PAPI  extends PlaceholderExpansion {
    private DailyTask plugin;
    public PAPI(DailyTask plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean persist() {
        return true;
    }

    @Override
    public boolean canRegister() {
        return true;
    }
    @Override
    public String getAuthor() {
        return plugin.getDescription().getAuthors().toString();
    }


    @Override
    public String getIdentifier() {
        return "Task";
    }

    @Override
    public String getVersion() {
        return plugin.getDescription().getVersion();
    }

    //%Task_Task1_conditions_id1_count% 当前任务条件完成度
    //%Task_Task1_conditions_id1_max% 当前任务条件数量要求
    //%Task_TodayGetCount% 玩家今日已领取任务数量
    //%Task_MaxGetCount% 玩家每日最大领取任务数量
    //%Task_Task1_Time% 玩家每日最大领取任务数量

    @Override
    public String onPlaceholderRequest(Player player, String identifier) {
        String[] args = identifier.split("_");
        if(args.length>=2) {
            Task task = DailyTask.getTask(args[0]);
            if(task==null){
                return identifier;
            }
            if(args[1].equalsIgnoreCase("Time")){
                try {
                    PlayerData  playerData = DailyTask.getPlayerData(player);
                   return task.getTiemLimitString(playerData);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            if(args[1].equalsIgnoreCase("conditions")) {
                Conditions conditions = task.getConditions(args[2]);
                if (conditions == null) {
                    return identifier;
                }

                if (args[3].equalsIgnoreCase("count")) {
                    PlayerData playerData = null;
                    try {
                        playerData = DailyTask.getPlayerData(player);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                    return String.valueOf(playerData.getCount(conditions));
                }
                if (args[3].equalsIgnoreCase("max")) {
                    return String.valueOf(conditions.getCount());
                }
            }
        }else if (args.length==1){
            if(args[0].equalsIgnoreCase("TodayGetCount")){
                PlayerData playerData = null;
                try {
                    playerData = DailyTask.getPlayerData(player);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
                return String.valueOf(playerData.getTaskCount());
            }
            if(args[0].equalsIgnoreCase("MaxGetCount")){
                return String.valueOf(DailyTask.plugin.getCount(player));
            }
        }

    return "";
    }
}
